package me.shurufa.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.model.Book;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 点击摄像机编辑跳转
 * 手动编辑 没有图片转换
 *
 * @FileName: me.shurufa.activities.BitmapNoTransformActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-20 10:53
 */
public class BitmapNoTransformActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.transform_edit)
    EditText transformEdit;
    @Bind(R.id.save_and_send)
    RadioButton saveAndSend;
    @Bind(R.id.save_no_send)
    RadioButton saveNoSend;
    @Bind(R.id.tramsform_rg)
    RadioGroup tramsformRg;
    @Bind(R.id.transform_take_photo)
    TextView transformTakePhoto;
    @Bind(R.id.transform_save_digest)
    TextView transformSaveDigest;
    @Bind(R.id.bottom_bar_layout)
    LinearLayout bottomBarLayout;
    @Bind(R.id.note_layout)
    LinearLayout noteLayout;
    @Bind(R.id.note_text)
    TextView noteText;

    private boolean isSend=true;
    String note;
    private InputMethodManager imm;
    private long bookId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_bitmap_no_transform;
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        bookId = getIntent().getLongExtra(Constants.ARG_BOOK_ID,0L);
        initToolBar();
        initUI();
    }

    private void initToolBar() {
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(getString(R.string.mark_down));
        titleTextView.setText(getString(R.string.make_digest));
        rightText.setOnClickListener(this);
    }

    private void initUI() {
        noteLayout.setVisibility(View.GONE);
        saveAndSend.setChecked(true);
        saveAndSend.setOnCheckedChangeListener(this);
        saveNoSend.setOnCheckedChangeListener(this);
        transformTakePhoto.setOnClickListener(this);
        transformSaveDigest.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.transform_take_photo:
                intent = new Intent(BitmapNoTransformActivity.this,CameraActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.transform_save_digest:
                //
                if (checkDigest()) {
                    if (bookId==0L) {
                        intent = new Intent(BitmapNoTransformActivity.this, AddDigestActivity.class);
                        intent.putExtra(Constants.ARG_DIGEST_CONTENT, transformEdit.getText().toString().trim());
                        intent.putExtra(Constants.ARG_DIGEST_STATE, isSend);
                        if (!TextUtils.isEmpty(note))
                            intent.putExtra(Constants.ARG_DIGEST_NOTE, note);
                    }else {
                        intent = new Intent(BitmapNoTransformActivity.this,AddDigestMenuActivity.class);
                        Book book = new Book();
                        book.book_id = bookId;
                        intent.putExtra(Constants.ARG_DIGEST_CONTENT, transformEdit.getText().toString().trim());
                        intent.putExtra(Constants.ARG_DIGEST_STATE, isSend);
                        if (!TextUtils.isEmpty(note))
                            intent.putExtra(Constants.ARG_DIGEST_NOTE, note);
                        intent.putExtra(Constants.ARG_BOOK,book.toString());
                    }
                    startActivityForResult(intent, Constants.REQUEST_SKIP_BOOKCONIFG_CODE);
                }
                break;
            case R.id.right_text:
//                intent = new Intent(Constants.ACTION_MAIN_SWITCH);
//                intent.putExtra(Constants.EXTRA_SWITCH_INDEX,0);
//                EventBus.getDefault().post(intent);
//                finish();
                intent = new Intent(BitmapNoTransformActivity.this,WriteNoteActivity.class);
                if (!TextUtils.isEmpty(note))
                    intent.putExtra(Constants.ARG_DIGEST_NOTE,note);
                startActivityForResult(intent,Constants.REQUEST_NOTE_CODE);
                break;
        }
    }

    private boolean checkDigest() {
        String text = transformEdit.getText().toString().trim();
        if (TextUtils.isEmpty(text)){
            Utils.showToast(R.string.digest_content_not_be_empty);
            return false;
        }else if (text.length()<10){
            Utils.showToast(R.string.content_must_greater_than_10);
            return false;
        }else
            return true;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.save_and_send:
                if (isChecked)
                    isSend = true;
                else
                    isSend = false;
                break;
            case R.id.save_no_send:
                if (isChecked)
                    isSend = false;
                else
                    isSend = true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(transformEdit.getWindowToken(), 0);
        if (requestCode ==Constants.REQUEST_NOTE_CODE&&resultCode==RESULT_OK) {
            note = data.getStringExtra(Constants.ARG_DIGEST_NOTE);
            if (!TextUtils.isEmpty(note)) {
                noteLayout.setVisibility(View.VISIBLE);
                noteText.setText(note);
            } else {
                noteLayout.setVisibility(View.GONE);
                noteText.setText("");
            }
            //下方判断预留
        }else if (requestCode == Constants.REQUEST_SKIP_BOOKCONIFG_CODE&&requestCode==RESULT_OK)
            finish();
    }

    @Override
    public void onEventMainThread(Intent intent) {
        super.onEventMainThread(intent);
        if (intent.getAction().equals(Constants.ACTION_ADD_DIGEST_COMPLETE))
            finish();
    }
}
